const host="https://ymy138.cn/base/"
//const host="http://127.0.0.1:1001//base/"
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const request = function (method, url, data) {
  let token = wx.getStorageSync('token');
  if(!token){
    wx.showModal({
      title: '提示',
      content: "请先登录",
      success (res) {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/login/login',
            complete:function(r){
              console.log("tiaoz",r)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: host + url,
      header: {
        token: token // 使用正确的 token 值
      },
      method: method,
      data: data,
      success: function (res) {
       if(res.data.code==1){
         resolve(res.data);
       }else{
         wx.showToast({
           title: res.data.info,
           icon:"none"
         })
       }

      },
      fail: function (res) {
        console.log("request1", res);
        reject(res);
      }
    });
  });
};

module.exports = {
  formatTime,
  request
}
